///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
#include <Model/VertexBuffer.h>

//-----------------------------------------------------------------------------

VertexBuffer::VertexBuffer()
{
	m_noComponents = 0;
	m_componentSize = 0;
	m_dataPtr = NULL;
}

//-----------------------------------------------------------------------------

VertexBuffer::~VertexBuffer()
{
	ReleaseData();
}

//-----------------------------------------------------------------------------

void	VertexBuffer::AddComponent( eVertexDataType	type, eVertexComponentType	componentType,u8	sizeComponent, u8	noComponents	)
{
	VertexInfo	newVI;
	newVI.type = type;
	newVI.offset = m_componentSize;
	newVI.sizeComponent = sizeComponent;
	newVI.noComponents = noComponents;
	newVI.componentType = componentType;

	m_componentSize += noComponents * sizeComponent;

	m_contents.push_back( newVI );
}

//-----------------------------------------------------------------------------

u8		VertexBuffer::GetComponentSize()
{
	return m_componentSize;
}

//-----------------------------------------------------------------------------

void	VertexBuffer::InitData(u32 noComponents)
{
	if( m_dataPtr )
	{
		ReleaseData();
	}

	ASSERT( noComponents > 0 );
	m_noComponents = noComponents;

	ASSERT( m_contents.size() > 0 );

	m_dataPtr = (void*) MGL_NEW u8[m_componentSize * m_noComponents];
	ASSERT( m_dataPtr );

	//moving position to the end of the components vector;
	std::vector<VertexInfo>::iterator it = m_contents.begin();
	for( ; it != m_contents.end(); it++ )
	{
		if( it->type == eVDT_POSITION )
		{
			VertexInfo save_vi = *it;
			m_contents.erase( it );
			m_contents.push_back( save_vi );
			break;
		}
	}
}

//-----------------------------------------------------------------------------

void	VertexBuffer::ReleaseData()
{
	SAFE_DEL_ARRAY( m_dataPtr );
}

//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
